package com.bank.chat.factory;

import com.bank.model.entity.ChatRecord;
import com.bank.model.vo.chat.ChatRecordVo;
import com.bank.model.vo.chat.UserListVo;
import com.bank.model.vo.user.UserBasicInfoVo;
import lombok.Getter;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author callumEm
 * @version 1.0
 * @create 2023/4/15 11:16
 */
@Component
@Getter
public class VoFactory {
    ChatRecordVo chatRecordVo;
    UserBasicInfoVo userBasicInfoVo;
    UserListVo userListVo;

    private void initChatRecordVo(){
        this.chatRecordVo = new ChatRecordVo();
        this.chatRecordVo.setDirection(true);
    }
    public ChatRecordVo getChatRecordVo(ChatRecord chatRecord,Long userId){
        initChatRecordVo();
        this.chatRecordVo.setId(chatRecord.getId());
        this.chatRecordVo.setFromId(chatRecord.getFromId());
        this.chatRecordVo.setToId(chatRecord.getToId());
        this.chatRecordVo.setContent(chatRecord.getContent());
        this.chatRecordVo.setTypeEnum(chatRecord.getTypeEnum());
        this.chatRecordVo.setCreatedTime(chatRecord.getCreatedTime());
        if(userId == chatRecord.getToId()){
            this.chatRecordVo.setDirection(false);
        }
        return this.chatRecordVo;
    }
    public List<ChatRecordVo> getChatRecordVos(List<ChatRecord> chatRecords, Long userId){
        List<ChatRecordVo> chatRecordVos = new ArrayList<>();
        for(ChatRecord chatRecord:chatRecords){
            chatRecordVos.add(getChatRecordVo(chatRecord,userId));
        }
        return chatRecordVos;
    }
    private void initUserBasicInfoVo(){
        this.userBasicInfoVo = new UserBasicInfoVo();
    }
    public UserBasicInfoVo getUserBasicInfoVo(String name){
        initUserBasicInfoVo();
        this.userBasicInfoVo.setUsername(name);
        return this.userBasicInfoVo;
    }
    private void initUserListVo(){
        this.userListVo = new UserListVo();
        this.userListVo.setName("none");
        this.userListVo.setUserListVos(new ArrayList<>());
    }
    public UserListVo getUserListVo(String name){
        initUserListVo();
        this.userListVo.setName(name);
        this.userListVo.setUserBasicInfoVo(getUserBasicInfoVo(name));
        return this.userListVo;
    }
    public UserListVo getUserListVo(UserBasicInfoVo userBasicInfoVo){
        initUserListVo();
        this.userListVo.setName(userBasicInfoVo.getUsername());
        this.userListVo.setUserBasicInfoVo(userBasicInfoVo);
        return this.userListVo;
    }
    public List<UserListVo> getUserListVos(List<UserBasicInfoVo> userBasicInfoVos){
        List<UserListVo> userListVos = new ArrayList<>();

        for(UserBasicInfoVo p: userBasicInfoVos){
            userListVos.add(getUserListVo(p));
        }
        return userListVos;
    }
}
